import keyConst from "../constant/key.const"
import SettingsToggler from "../utils/SettingsToggler"
import MeasurementModeData from "../data/MeasurementModeData"
import FloorsViewData from "../data/FloorsViewData"
import SweepData from "../data/SweepData"
import ViewmodeData from "../data/ViewmodeData"
import {
  MeasurementModeToggle,
  MeasurementRenameCommand,
  MeasurementVisibilityCommand,
  MeasurementListDeletionCommand,
  MeasurementSelectById
} from "../command/measurements.command"
import SettingsData from "../data/SettingsData"
import EngineContext from "../EngineContext"
export default class MeasurementsManager {
  engine: EngineContext
  initialized: boolean
  disabledAssets: Record<string, boolean>
  settingsToggler: SettingsToggler
  dataMap: { measurementModeData: MeasurementModeData; floorsViewData: FloorsViewData; sweepData: SweepData; viewmodeData: ViewmodeData }
  constructor(e: EngineContext, t: SettingsData) {
    this.engine = e
    this.initialized = !1
    this.disabledAssets = {
      [keyConst.rule.features.sweep]: !1,
      [keyConst.rule.features.views]: !1,
      [keyConst.rule.features.labels]: !1,
      [keyConst.rule.features.mattertags]: !1
    }
    this.settingsToggler = new SettingsToggler(t, this.disabledAssets)
  }
  async activate() {
    if (!this.initialized) {
      this.initialized = !0
      const e = this.engine
      const [t, i, n, s] = await Promise.all([
        e.market.waitForData(MeasurementModeData),
        e.market.waitForData(FloorsViewData),
        e.market.waitForData(SweepData),
        e.market.waitForData(ViewmodeData)
      ])
      this.dataMap = {
        measurementModeData: t,
        floorsViewData: i,
        sweepData: n,
        viewmodeData: s
      }
    }
    this.settingsToggler.toggle(!0)
    await this.engine.commandBinder.issueCommandWhenBound(new MeasurementModeToggle(!0, !0))
  }
  async deactivate() {
    this.settingsToggler.toggle(!1)
    this.dataMap.measurementModeData.setSelectedGroupIndex(-1)
    await this.engine.commandBinder.issueCommand(new MeasurementModeToggle(!1))
  }
  in360view() {
    const e = this.dataMap.sweepData.currentSweep ? this.dataMap.sweepData.currentSweep : ""
    return this.dataMap.viewmodeData.isInside() && this.dataMap.sweepData.isSweepUnaligned(e)
  }
  getSelectedMeasurement() {
    return this.dataMap.measurementModeData.selectedGroupSid
  }
  renameMeasurement(e, t) {
    return this.engine.commandBinder.issueCommand(new MeasurementRenameCommand(e, t))
  }
  setMeasurementVisibility(e, ...t) {
    return this.engine.commandBinder.issueCommand(new MeasurementVisibilityCommand(e, ...t))
  }
  async batchDeleteMeasurement(e) {
    return this.engine.commandBinder.issueCommand(new MeasurementListDeletionCommand(...e))
  }
  async deleteMeasurement(e) {
    await this.engine.commandBinder.issueCommand(new MeasurementListDeletionCommand(e))
  }
  async toggleSelected(e) {
    await this.engine.commandBinder.issueCommand(new MeasurementSelectById(e))
  }
}
